<?php

  session_start();
  $allowed_page = true;
  
  $point_color='#999';
  
  
  function __autoload($className) {
  if (!isset($allowed_page)) { $allowed_page = true; }
    require_once("../lib/config.php");
    require_once(CLASS_PATH."{$className}.class.php");
  }
  if(isset($_GET['mid'])) {
    $map = new Map();
    $map->loadMap($_GET['mid']);
    $_SESSION['admin_map'] = $map;
  }elseif(isset($_POST['points'])) {
    $map = $_SESSION['admin_map'];
    $map->setCollisionPoints(str_replace(",","",$_POST['points']));
    $map->save();
    header("Location: admin_maps.php");
  }else {
    exit;
  }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>
            D5Power Web Game Engine - 管理员
        </title>
        <link rel="stylesheet" href="../../css/admin_styles.css" type="text/css">
        <script type="text/javascript">
            var collisionPoints = [];

            function setCollision(object) {
                if (object.style.background != "") {
                    object.style.background = "";
                }
                else {
                    object.style.background = "<?php echo $point_color;?>";
                }
                return;
            }

            function createCollisionString() {
                for(i=0; i<1200; i++) {
                    cell = document.getElementById(i);
                    if (cell.style.background != "") {
                        collisionPoints[i] = 1;
                    }
                    else {
                        collisionPoints[i] = 0;
                    }
                }
                var form = document.getElementById('form_edit_collision');
                var points_field = document.createElement('INPUT');
                    points_field.setAttribute("type","hidden");
                    points_field.setAttribute("name","points");
                    points_field.setAttribute("value",collisionPoints.toString());
                form.appendChild(points_field);
                form.submit();
                return;
            }
        </script>
    </head>
    <body style="color: rgb(255, 255, 230); background-color: rgb(0, 0, 0); font-family: 'Trebuchet MS',Verdana,Tahoma,Arial;">
        <div id="main" align="center">
            <div id="container">

            <form id="form_edit_collision" method="POST" action="admin_map_collision.php">
            </form>

                <div id="menu">
                    <p align="center"><i><b><font size="5">管理系统</font></b></i><font size="4"><b><br>
                        D5Power 网页游戏引擎</b></font>
                    </p>
                    <p align="center"><b><font color="#ffffff" size="2">管理中心</font></b>
                    </p>
                    <p align="center"><b><font size="1"><u>退出系统</u></font></b>
                    </p>
                </div>

                <div id="panel">
                    <img id="img_title" src="../../pictures/cenario.png" border="0" height="40" width="40">
                    <h1>欢迎使用D5Power网页游戏引擎</h1>
                    <h2><a href="admin.php">管理员</a>&gt; <a href="admin_maps.php">地图管理器</a> &gt; 定义地图路径点<font color="#ffffff"><b><font size="1">&nbsp;</font></b>&nbsp;</font><br>
                    </h2>
                </div>

                <div id="contents" style="width:640px;height:480px">
                    <br>
                    <div id="water_layer" style="position: absolute; top: inherit; left: inherit; width: 640px; height: 480px; z-index: 1; background-image: url(../../pictures/maps/<?php print($map->getTerrainLayer()); ?>);"></div>
                    <div id="ground_layer" style="position: absolute; top: inherit; left: inherit; width: 640px; height: 480px; z-index: 2; background-image: url(../../pictures/maps/<?php print($map->getGroundLayer()); ?>);"></div>
                    <div id="up_layer" style="position: absolute; top: inherit; left: inherit; width: 640px; height: 480px; z-index: 3; background-image: url(../../pictures/maps/<?php print($map->getUpLayer()); ?>);"></div>
                    <div class="collision_block" style="z-index: 4; position: relative; top: inherit; left: inherit; width: 640px;">
                    <?php
                        print("<table rules=\"box\" class=\"table_collision_points\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");

                        $cell_id = 0;
                        $points = str_split($map->getCollisionPoints());
                        for($i=0; $i<30; $i++) {
                            print("<tr>\n");
                            for($j=0; $j<40; $j++) {
                              if($points[$cell_id] == 1) {
                                print("<td style=\"background: ".$point_color.";\" id=\"".$cell_id."\" onclick=\"setCollision(this);\"></td>\n");
                              }
                              else {
                                print("<td id=\"".$cell_id."\" onclick=\"setCollision(this);\"></td>\n");
                              }
                              $cell_id++;
                            }
                            print("</tr>\n");
                        }
                        print("</table>");
                    ?>
                    </div>
                    <br>
                    <input onClick="createCollisionString();" type="button" name="btn_enviar" value="保存地图路径点">
                </div>
            </div>
        </div>
    </body>
</html>